/*
 * 用户信息操作模块测试代码
 * BUG: 删除用户 修改用户 查看用户只能看第一个
 */

#include "usbctrl.h"
#include "../include/clientinfo.h"

void
mysignal()
{
  printf("抓到信号,保存并退出\n");
  closeci();
  //  system("echo aaaaa >tempfile");
  exit(0);
}

int
main(int argc,char **argv)
{

  int	choose;
  struct clientinfo ci,allci[MAXCLIENTS];
  if(initci()<0){
    printf("初始化失败");
    exit(0);
  }
  signal(SIGINT,mysignal);
  signal(SIGTERM,mysignal);
  while(1){
    printf("\n    1.添加用户:\n");
    printf("    2.删除用户:\n");
    printf("    3.修改用户:\n");
    printf("    4.查看用户:\n");
    printf("    0.退出:\n");
    printf("选择操作: ");
    scanf("%d",&choose);
    memset(&ci,0,sizeof(struct clientinfo));
    printf("\n");
    switch(choose){
    case 1:
      
      printf("添加用户");
      printf("\n输入用户IP:");
      scanf("%d",&ci.ip);
      printf("\n添加中...\n");
      if(addci(ci)<0)
	printf("添加失败\n");
      else
	printf("添加成功\n");
      break;
    case 2:
      printf("删除用户:\n");
      printf("\n输入用户IP:");
      scanf("%d",&ci.ip);
      printf("\n删除中...\n");
       if(delci(ci)<0)
	printf("删除失败\n");
      else
	printf("删除成功\n");
      break;
    case 3:
      printf("修改用户:\n");
      printf("\n输入用户IP:");
      scanf("%d",&ci.ip);
      printf("\n输入目标usb状态:");
      scanf("%d",&ci.stat);
      printf("\n输入使用:");
      scanf("%d",&ci.utime);
      printf("\n修改中...\n");
       if(setci(ci)<0)
	printf("修改失败\n");
      else
	printf("修改成功\n");
      break;
    case 4:
      printf("查看用户:\n");
      printf("输入用户IP(输入0则查看全部)");
      scanf("%d",&ci.ip);
      int	n;
      if((n=getci(ci,allci))<0)
	printf("查看失败\n");
      else
	while(n > 0){
	  printf("IP: %d, STAT: %d\n",allci[MAXCLIENTS-n].ip,allci[MAXCLIENTS-n].stat);
	  n--;
	}
      break;
    case 0:
      printf("退出:\n");
      return 0;
    default:
      exit(0);
    }
  }
  return 0;
}
